<template>
  <div 
    :class="['book-card', `view-${viewMode}`]" 
    @click="$emit('click')"
  >
    <div class="cover-container">
      <img 
        :src="book.coverImage || '/images/default-book-cover.jpg'" 
        :alt="`Cover of ${book.title}`" 
        class="cover-image"
      />
      <div class="status-badge" :class="book.status">
        {{ statusLabel }}
      </div>
    </div>
    <div class="book-info">
      <h3 class="book-title">{{ book.title }}</h3>
      <p class="book-author">by {{ book.author }}</p>
      
      <div class="book-meta">
        <div class="category-tags">
          <el-tag 
            v-for="category in displayCategories" 
            :key="category" 
            size="mini"
            type="info"
            effect="plain"
          >
            {{ category }}
          </el-tag>
          <el-tag v-if="hasMoreCategories" size="mini" type="info">+{{ book.categories.length - maxCategories }}</el-tag>
        </div>
        
        <div v-if="viewMode === 'list'" class="book-description">
          {{ truncatedDescription }}
        </div>
        
        <div class="book-details">
          <span class="publish-date">
            <i class="el-icon-date"></i> {{ formattedDate }}
          </span>
          <span v-if="book.isbn" class="isbn">
            <i class="el-icon-document"></i> ISBN: {{ book.isbn }}
          </span>
        </div>
      </div>
      
      <div class="book-actions">
        <el-button size="small" type="primary" icon="el-icon-view" @click.stop="$emit('view')">
          View
        </el-button>
        <el-button size="small" icon="el-icon-star-off" @click.stop="$emit('favorite')">
          {{ book.isFavorite ? 'Unfavorite' : 'Favorite' }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'BookCard',
  props: {
    book: {
      type: Object,
      required: true
    },
    viewMode: {
      type: String,
      default: 'grid',
      validator: value => ['grid', 'list'].includes(value)
    }
  },
  data() {
    return {
      maxCategories: 2,
      maxDescriptionLength: 150
    }
  },
  computed: {
    statusLabel() {
      const statusMap = {
        'available': 'Available',
        'borrowed': 'Borrowed',
        'reserved': 'Reserved',
        'maintenance': 'Maintenance'
      }
      return statusMap[this.book.status] || this.book.status
    },
    displayCategories() {
      return this.book.categories.slice(0, this.maxCategories)
    },
    hasMoreCategories() {
      return this.book.categories.length > this.maxCategories
    },
    truncatedDescription() {
      if (!this.book.description) return ''
      if (this.book.description.length <= this.maxDescriptionLength) {
        return this.book.description
      }
      return this.book.description.substring(0, this.maxDescriptionLength) + '...'
    },
    formattedDate() {
      if (!this.book.publishDate) return 'Unknown'
      const date = new Date(this.book.publishDate)
      return date.toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' })
    }
  }
}
</script>

<style lang="scss" scoped>
.book-card {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  overflow: hidden;
  transition: all 0.3s ease;
  height: 100%;
  display: flex;
  flex-direction: column;
  
  &:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    transform: translateY(-4px);
  }

  .card-img {
    height: 220px;
    overflow: hidden;
    position: relative;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.5s ease;
    }
    
    .status-badge {
      position: absolute;
      top: 8px;
      right: 8px;
      z-index: 2;
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 12px;
      font-weight: 600;
      
      &.available {
        background-color: #67c23a;
        color: white;
      }
      
      &.borrowed {
        background-color: #e6a23c;
        color: white;
      }
      
      &.reserved {
        background-color: #f56c6c;
        color: white;
      }
    }
    
    .favorite-btn {
      position: absolute;
      bottom: 8px;
      right: 8px;
      z-index: 2;
      cursor: pointer;
      color: #ff9800;
      background: rgba(255, 255, 255, 0.8);
      border-radius: 50%;
      padding: 6px;
      font-size: 18px;
      transition: all 0.2s ease;
      
      &:hover {
        transform: scale(1.1);
      }
    }
  }
  
  .card-content {
    padding: 12px;
    flex: 1;
    display: flex;
    flex-direction: column;
    
    .book-title {
      font-size: 16px;
      font-weight: 600;
      margin-bottom: 6px;
      line-height: 1.3;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    
    .book-author {
      font-size: 14px;
      color: #606266;
      margin-bottom: 8px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    .book-categories {
      display: flex;
      flex-wrap: wrap;
      gap: 4px;
      margin-bottom: 8px;
      
      .category {
        padding: 2px 6px;
        background-color: #f0f2f5;
        border-radius: 4px;
        font-size: 12px;
        color: #606266;
      }
    }
    
    .book-description {
      font-size: 13px;
      color: #909399;
      margin-bottom: 12px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 3;
      -webkit-box-orient: vertical;
      flex: 1;
    }
    
    .card-actions {
      display: flex;
      justify-content: space-between;
      margin-top: auto;
      
      .el-button {
        font-size: 13px;
        padding: 7px 14px;
      }
    }
  }
  
  &.list-view {
    flex-direction: row;
    height: auto;
    
    .card-img {
      width: 120px;
      height: 160px;
      flex-shrink: 0;
    }
    
    .card-content {
      padding: 12px 16px;
      
      .book-title {
        font-size: 18px;
        margin-bottom: 8px;
      }
      
      .book-author {
        font-size: 15px;
        margin-bottom: 10px;
      }
      
      .book-categories {
        margin-bottom: 10px;
      }
      
      .book-description {
        -webkit-line-clamp: 4;
        margin-bottom: 16px;
      }
    }
  }
}

@media screen and (max-width: 768px) {
  .book-card {
    &.view-list {
      .cover-container {
        width: 70px;
        min-width: 70px;
        height: 110px;
      }
      
      .book-info {
        padding: 10px;
        
        .book-title {
          font-size: 14px;
        }
        
        .book-author {
          font-size: 12px;
          margin-bottom: 10px;
        }
        
        .book-description {
          display: none;
        }
        
        .book-actions {
          flex-direction: column;
          gap: 6px;
          
          .el-button {
            width: 100%;
          }
        }
      }
    }
  }
}
</style> 